package top.heliming.youyou.item.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.heliming.youyou.item.pojo.SpecGroup;
import top.heliming.youyou.item.pojo.SpecParam;
import top.heliming.youyou.item.pojo.Specification;
import top.heliming.youyou.item.service.SpecificationService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/30 下午 9:28
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {

  @Autowired
  private SpecificationService specificationService;

//  @GetMapping("{id}")
//  @ResponseBody
//  public ResponseEntity<String> querySpecificationByCategoryId(@PathVariable("id") Long id){
//    Specification spec = this.specificationService.queryById(id);
//    if (spec == null) {
//      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
//    }
//    return ResponseEntity.ok(spec.getSpecifications());
//  }
  @GetMapping("/params")
  public ResponseEntity<List<SpecParam>> querySpecParam(
      @RequestParam(value="gid", required = false) Long gid,
      @RequestParam(value="cid", required = false) Long cid,
      @RequestParam(value="searching", required = false) Boolean searching,
      @RequestParam(value="generic", required = false) Boolean generic
  ){
    List<SpecParam> list =
        this.specificationService.querySpecParams(gid,cid,searching,generic);
    if(list == null || list.size() == 0){
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }

  /**
   * 根据商品分类ID查询规格组
   *
   * @param cid
   * @return
   */
  @GetMapping("groups/{cid}")
  public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid") Long cid) {
    return ResponseEntity.ok(specificationService.querySpecGroupByCid(cid));
  }

  /**
   * 查询规格参数组，及组内参数
   *
   * @param cid
   * @return
   */
  @GetMapping("{cid}")
  public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid){
    List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);
    if(list == null || list.size() == 0){
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }
}
